home *** CD-ROM | disk | FTP | other *** search
- /* openix.c - open / close an index file for BTREE */
- #include "stdio.h"
- #include "cminor.h"
- #include "btree.h"
- #include "bt_macro.h"
-
- extern IX_DESC *pci ; /* refers to index descriptor */
- /* for current function call */
-
- RECPOS wrt_dum() ;
-
- int openix(name,pix,cfun,sfun) /* open an existing index file */
- char name[] ; /* file name */
- IX_DESC *pix ; /* control block for index */
- int (*cfun) () ; /* address of compare function */
- int (*sfun) () ; /* address of entry size function */
- {
- int ret ;
-
- pci = pix ;
- ret = open_if(name) ;
- if( ret < 0 ) /* check for failure to create file */
- return( IX_FAIL ) ;
- pci->ixfile = ret ;
- read_if(0L,&pix->dx, sizeof(IX_DISK) ) ;
- pci->pcomp = cfun ; /* record address of compare fun. */
- pci->psize = sfun ; /* and of entry size function */
- init_bio() ; /* initialize block I/O */
- go_first(pix) ; /* position at start of file */
- return( IX_OK ) ;
- }
-
- int close(pix) /* close an index file */
- IX_DESC *pix ; /* index descriptor */
- {
- pci = pix ;
- write_if(0L,&pci->dx, sizeof( IX_DISK ) ) ;
- close_if(); /* close the index file */
- }
-
-
- int creatix(name,filesize,pdum,ndum) /* create a new index file */
- char name[] ; /* name of file */
- long filesize ; /* size of the file */
- ENTRY *pdum ; /* dummy entry for EOF */
- int ndum ; /* size of dummy entry */
- {
- BLOCK b ;
- IX_DESC ixd ;
- int ret ;
-
- pci = & ixd ;
- ret = creat_if(name) ;
- if( ret < 0 ) /* check for failure to create file */
- return( IX_FAIL ) ;
- ixd.ixfile = ret ;
- ixd.dx.nl = MAX_LEVELS ; /* all levels present */
- mover(&ixd.dx.dume,pdum,ndum) ;
- clr_mem(&b,0,IXB_SIZE) ; /* make a block of zeros */
- write_block(0L,&b) ; /* write it at BOF */
- /* set up index block for each level */
- /* and record location of root block */
- ixd.dx.rb = wrt_dum(pdum,ndum) ;
- /* set up free block list */
- /* start it after dummy blocks */
- make_free( ((RECPOS) (MAX_LEVELS+1)) * IXB_SIZE, filesize) ;
- closeix(&ixd) ; /* close file updating desc. info */
- return( IX_OK ) ;
- }
-
- RECPOS wrt_dum(pdum,ndum) /* write index block for each level */
- ENTRY *pdum ; /* dummy entry */
- int ndum ; /* size of dummy entry */
- {
- BLOCK b ;
- int l ;
- RECPOS r ;
-
- pdum->rptr = NULLREC ;
- r = 0 ;
- for( l=0 ; l < MAX_LEVELS ; l=l+1 )
- { r = r + IXB_SIZE ;
- mover( b.entries,pdum,ndum) ;/* put dummy in block */
- b.lvl = l ;
- b.bend = ndum ; /* block contains one entry */
- write_block(r,&b) ; /* write the block */
- pdum->rptr = r ;
- }
- return( r ) ;
- }
-
-
- int removeix(name) /* delete an index file */
- char name[] ; /* name of the file */
- {
- unlink(name) ;
- }
-
-
-